package com.coollang.smartbra.presenter;

import android.net.Uri;
import android.widget.Toast;

import com.coollang.smartbra.activity.SelfInfoActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.model.LogInDataBean;
import com.coollang.smartbra.model.LoginModel;
import com.coollang.smartbra.model.UploadPhotoModel;
import com.coollang.smartbra.utils.ImageLoader;
import com.coollang.smartbra.utils.MD5Utils;
import com.coollang.smartbra.utils.TimeUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author : Timmy
 * @Data: 2017/7/11
 * @Fuction:
 */
public class SelfInfoPresenter extends BasePresenter<LoginModel> {

    private SelfInfoActivity mSelfInfoActivity;

    public SelfInfoPresenter(SelfInfoActivity infoActivity) {
        super(LoginModel.class);
        mSelfInfoActivity = infoActivity;
    }

    @Override
    public void setRequestInfo() {
        setBaseOkgo("user/update-user-info");
    }

    @Override
    protected Map<String, String> getData() {
        Map<String, String> map = new HashMap<>(16);

        LogInDataBean mLogInDataBean = MyApplication.mLogInDataBean;
        map.put("id", mLogInDataBean.getId());
        map.put("username", mLogInDataBean.getUsername());
        map.put("gender", mLogInDataBean.getGender());
        map.put("birth", mLogInDataBean.getBirth() + "");
        map.put("height", mLogInDataBean.getHeight());
        map.put("weight", mLogInDataBean.getWeight());
        map.put("nickname", mLogInDataBean.getNickname());
        return map;
    }

    @Override
    protected void parsrData(LoginModel model) {

        int code = Integer.parseInt(model.getCode());
        if (code == 200) {
            // 修改成功
            mSelfInfoActivity.changeUserInfoSucceed();
        }
    }

    // 上传头像
    public void uploadHeadImage(File file) {
        String url = "user/upload-photo";

        String timeStamp = TimeUtils.getCurrentTimeUnixString();
        String sign = MD5Utils.getMD5(url.split("/")[1] + timeStamp);


        OkGo.post(Constants.HOST + url)
                .tag(mSelfInfoActivity)
                .params("timestamp", timeStamp)
                .params("sign", sign)
                .params("username", MyApplication.mLogInDataBean.getUsername())
                .params("file", file)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        UploadPhotoModel model = mGson.fromJson(s, UploadPhotoModel.class);
                        String code = model.getCode();
                        if ("200".equals(code)) {
                            String data = model.getData();
                            ImageLoader.clearCache(Uri.parse(MyApplication.mLogInDataBean.getAvatar()));

                            MyApplication.mLogInDataBean.setAvatar(data);
                            mSelfInfoActivity.upLoadHradImageSucceed(data);
                        } else {
                            onError(call, response, new RuntimeException("上传失败"));
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(mSelfInfoActivity, "请检查当前网络", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
